import java.util.Stack;

public class Ygj {

    public static void main(String[] args) {
        Queue<Integer> mQueue = new StackImplQueue<>();
        mQueue.push(5);
        mQueue.push(6);
        mQueue.push(7);
        mQueue.push(8);
        mQueue.push(9);
        mQueue.push(10);

        while (!mQueue.empty()) {
            System.out.println("队列是否为空：" + mQueue.empty());
            System.out.println("首部元素：" + mQueue.peek());
            System.out.println("移除的首部元素：" + mQueue.pop());
        }
        System.out.println("队列是否为空：" + mQueue.empty());
    }

    public interface Queue<E> {
        /**
         * 将一个元素放入队列的尾部
         * 
         * @param e 放入队列元素
         */
        void push(E e);

        /**
         * 从队列首部移除元素
         * 
         * @return 队列收不元素
         */
        E pop();

        /**
         * 返回队列首部的元素
         * 
         * @return 队列首部的元素
         */
        E peek();

        /**
         * 返回队列是否为空
         * 
         * @return true:队列为空，false:队列不为空
         */
        boolean empty();
    }

    public static class StackImplQueue<E> implements Queue<E> {
        private Stack<E> mStackPush;
        private Stack<E> mStackPop;

        public StackImplQueue() {
            mStackPush = new Stack<>();
            mStackPop = new Stack<>();
        }

        @Override
        public void push(E e) {
            mStackPush.push(e);
            pushToPop();

        }

        @Override
        public E pop() {
            if (mStackPush.isEmpty() && mStackPop.isEmpty())
                throw new RuntimeException("Queue is  empty");
            pushToPop();
            return mStackPop.pop();
        }

        @Override
        public E peek() {
            if (mStackPush.isEmpty() && mStackPop.isEmpty())
                throw new RuntimeException("Queue is empty");
            pushToPop();
            return mStackPop.peek();
        }

        @Override
        public boolean empty() {
            return mStackPush.isEmpty() && mStackPop.isEmpty();
        }

        private void pushToPop() {
            if (mStackPop.isEmpty()) {
                while (!mStackPush.isEmpty()) {
                    mStackPop.push(mStackPush.pop());
                }
            }
        }
    }
}
